export const triggers = [];
export const obj = {
    before: null,
    isLeft: false,
    isRight: false,
    isA: false,
    isD: false,
    isUp: false,
    isBottom: false
}

export function addTriger(trigger) {
    triggers.push(trigger);
}


export class IOSuper {
    constructor() {
        // 每次按键都要触发trigger中的所有事件
        this.triggers = [];

        this.nowState = null;
        this.initNowState();

    }

    addTrigger(trigger) {
        this.triggers.push(trigger);
    }

    execTrigger() {
        this.triggers.forEach(trigger => {
            trigger(this.getNowState.bind(this), this.getBackupNowState.bind(this));
        })
    }

    backupNowState() {
        this.nowState.before = {...this.nowState};
    }

    setNowState(key, bool) {
        this.nowState[key] = bool;
    }

    getNowState(key) {
        return this.nowState[key];
    }

    getBackupNowState(key) {
        return this.nowState.before[key];
    }

    initNowState() {
        this.nowState = {
            before: null,
            left: false,
            right: false,
            a: false,
            d: false,
            up: false,
            down: false
        }
    }
    clearTriggers() {
        this.triggers.length = 0;
    } 
}